package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.dto.OrderDTO;
import com.example.demo.dto.OrderDetailDTO;
import com.example.demo.entity.OrdersManagement;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 订单管理 Mapper 接口
 * </p>
 */
@Mapper
public interface OrdersManagementMapper extends BaseMapper<OrdersManagement> {
    // 分页
    @Select("<script>" +
            "SELECT * FROM orders_management " +
            "<where>" +
            "   <if test='keyword != null and keyword != \"\"'>" +
            "       o_id LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR o_goods_id LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR o_user_id_one LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR o_user_id_two LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR o_total_amount LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR o_status LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR o_create_time LIKE CONCAT('%', #{keyword}, '%') " +
            "   </if>" +
            "</where>" +
            "</script>")
    IPage<OrdersManagement> selectOrdersPage(Page<OrdersManagement> page, @Param("keyword") String keyword);

    @Select("SELECT om.*, g.g_name AS productName, g.g_category AS category, " +
            "g.g_description AS description, g.g_price AS productPrice " +
            "FROM orders_management om " +
            "JOIN goods g ON om.o_goods_id = g.g_id " +
            "WHERE om.o_user_id_two = #{buyerId}")
    List<OrderDTO> selectBuyerOrdersWithDetails(Integer buyerId);

    @Select("SELECT om.*, g.g_name AS productName, g.g_category AS category, " +
            "g.g_description AS description, g.g_price AS productPrice " +
            "FROM orders_management om " +
            "JOIN goods g ON om.o_goods_id = g.g_id " +
            "WHERE om.o_user_id_one = #{sellerId}")
    List<OrderDTO> selectSellerOrdersWithDetails(Integer sellerId);

    @Select("SELECT om.*, " +
            "g.g_name AS productName, " +
            "g.g_category AS category, " +
            "g.g_description AS description, " +
            "g.g_price AS productPrice, " +
            "u1.u_name AS sellerName, " +
            "u2.u_name AS buyerName " +
            "FROM orders_management om " +
            "JOIN goods g ON om.o_goods_id = g.g_id " +
            "JOIN user u1 ON om.o_user_id_one = u1.u_id " +
            "JOIN user u2 ON om.o_user_id_two = u2.u_id " +
            "WHERE om.o_id = #{orderId}")
    OrderDetailDTO selectOrderDetailById(Integer orderId);

    // 取消订单并更新余额
    @Update("UPDATE orders_management SET o_status = '已取消' WHERE o_id = #{orderId}")
    int cancelOrder(@Param("orderId") Integer orderId);

    // 确认收货
    @Update("UPDATE orders_management SET o_status = '已完成' WHERE o_id = #{orderId}")
    int confirmOrder(@Param("orderId") Integer orderId);
}
